//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    listData: [],
    page: 1,
    loading: 0,
    lastPage: 0,
    loaded: 0,
    options:{},
    kw:'',
    user_info: {}
  },
  onLoad: function (options) {
    var that = this;
    this.data.options = options ? options:{};
    app.getUserInfo(function (user_info) {
      that.data.user_info = user_info;
      that.setData({ user_info: user_info});
      that.initData();
    })

  },
  initData: function (reload = 0) {
    if (reload == 1) {
      this.setData({
        listData: [],
        page: 1,
        loading: 0,
        lastPage: 0,
        loaded: 0,
      });
    }
    this.getList();

  },


del:function(e){
    var that = this;
    var idx = e.currentTarget.dataset.key;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
    title: '提示',
    content: '确认删除吗？',
    success: function(res) {
          if (res.confirm) {
            //删除评论
            app.api("api/community_bills/del",{
              bill_entry_id:id,
              method: 'POST'
              },function(res){
               //删了出错
                if (res.code!=200){
                    app.alert(res.msg);
                }else{
                    wx.showToast({
                      title: '删除成功',
                      icon: 'success',
                      duration: 2000
                    })
                    var listData=that.data.listData;
                    listData[idx]['none']=1;
                    that.setData({ listData: listData});
                }      
            });   
          }
         }
    })
  },


  billStatus:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/tenement/updatebills?id=' + id,
    })
    // var that = this;
    // var idx = e.currentTarget.dataset.key;
    // var id = e.currentTarget.dataset.id;
    // wx.showModal({
    // title: '提示',
    // content: '确认已收到款项了吗？',
    // success: function(res) {
    //       if (res.confirm) {
    //         //更新
    //         app.api("api/community_bills/billStatus",{
    //           bill_entry_id:id,
    //           method: 'POST'
    //           },function(res){
    //            //删了出错
    //             if (res.code!=200){
    //                 app.alert(res.msg);
    //             }else{
    //                 wx.showToast({
    //                   title: '操作成功',
    //                   icon: 'success',
    //                   duration: 2000
    //                 })
    //                 var listData=that.data.listData;
    //                 listData[idx]['status']=1;
    //                 that.setData({ listData: listData});
    //             }      
    //         });   
    //       }
    //      }
    // })
  },

  pay:function(e){
    console.log(e);
    var bill_entry_id = e.currentTarget.dataset.id;
    var that = this;
    that.setData({ loading: 1, disabled: 1 });
    that.data.payloading = 1;
    app.api("api/pay/community_bills_order", {
      bill_entry_id: bill_entry_id
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0 });
        that.setData({ 'disabled': 0 });
        return false;
      }
      var order_sn = res.data.order_sn;
      app.api("api/pay/topay", {
        order_sn: order_sn
      }, function (res) {
        if (res.data && res.data.error_code == "1001") {
          that.data.card_code = "";
        }
        if (res.code != 200) {
          app.alert(res.msg);
          that.data.payloading = 0;
          that.setData({ 'loading': 0 });
          that.setData({ 'disabled': 0 });
          return false;
        }

        if (res.data.over == 1) {
          that.suc();
          return;
        }
        var params = res.data;
        //params.total_fee = 0.1;
        params.success = function (res) {
          that.setData({ loading: 0, disabled: 0 });
          that.suc();
        }
        params.fail = function (res) {
          if (res.errMsg == 'requestPayment:fail cancel') {
            that.setData({ loading: 0, disabled: 0 });
            // wx.navigateTo({
            //   url: '/pages/order/detail?order_sn=' + order_sn,
            // })
            return false;
          }
          app.alert(res.errMsg);
        }
        console.log(params);
        wx.requestPayment(params)
      })
    })
  },

  suc:function(){
    this.initData(1);
  },
  
  moreAct:function(e){    
      var id = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
      //var key = e.currentTarget.dataset.key;
     // console.log(e);
      var itemListArray = [];
    if (this.data.user_info.tenement_type.company || this.data.user_info.tenement_type.employee) {
      itemListArray.push({ text: '修改', act: function(){
            wx.navigateTo({
              url: '/pages/tenement/addbills?id='+id,
            })
      } });
      //if (to_user_id == this.data.user_info.user_id || to_user_id==this.data.data.user_id){
        itemListArray.push({ text: '删除', act: this.del });
     // }
      itemListArray.push({ text: '线下收款', act: this.billStatus });
    } else if (status ==0){
      itemListArray.push({ text: '马上缴费', act: this.pay });
    }
    if (itemListArray.length==0){
      return ;
    }
      var itemList = [];
      //console.log(itemListArray);
      for (var i in itemListArray) {
        itemList.push(itemListArray[i].text);
      }
      wx.showActionSheet({
        itemList: itemList,
        success: function (res) {
          console.log(itemListArray[res.tapIndex]);
          itemListArray[res.tapIndex]['act'](e);
        },
        fail: function (res) {
          console.log(res.errMsg)
        }
      })
  },
  

  //提醒列表
  getList() {
    //通知提醒 
    var that = this;
    var page = this.data.page;
    this.setData({
      loading: 1
    });
    app.api("api/community_bills/get", {
      page: page,
      limit: 20,
      kw: this.data.kw,
      status: this.options.status ? this.options.status:'',
      user_id: this.options.user_id ? this.options.user_id:'',
      community_id: this.data.user_info.community.community_id
    }, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        if(res.code==6){
          wx.showModal({
            title: '身份验证',
            content: '通过手机号绑定业主身份后，才能查看账单',
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/user/bindphone',
                })
              } else if (res.cancel) {
                wx.navigateBack();
              }
            }
          })
         
        }else{
          app.alert(res.msg,function(){
            wx.navigateBack();
          });
        }
        return;
      }
      if (res.data.list.length == 0) {
        that.setData({
          lastPage: 1
        });
      }
      page++;
      if (res.data.list) {
        that.setData({ page: page, loading: 0, 'listData': that.data.listData.concat(res.data.list) });
      }
    });
  },

  onReachBottom: function () {
    this.initData();
  },


  onPullDownRefresh: function () {
    this.initData(1);
  },

  onPullDownRefresh: function () {
    this.initData(1);
  },
  onShow: function () {
    var reload = app.getPageData("tenement/bills");
    if (reload == 1) {
      this.initData(1);
      app.setPageData("tenement/bills", { reload: 0 });
    }
  },
  setsearchkw: function (event) {
    this.data.kw = event.detail.value;
    // console.log(this.data.kw);
  },
  search: function () {
    this.initData(1);
  }

})
